package program;

import java.io.File;

import kernel.FileManager;
import kernel.IOManager;
import kernel.exception.OSException;
import kernel.process.AbstractProcess;
import kernel.process.AbstractProcessHeader;

/**
 * Cd/Chdir process.
 * For more see man() method.
 * @author Petr Hajek, Veronika Vencurikova
 *
 */
public class Cd extends AbstractProcess {

	public Cd(AbstractProcessHeader header, long pid) {
		super(header, pid);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String doJob(String input) throws OSException {
		if (this.params.size() > 1) throw new OSException(OSException.ERR_MANY_ARGS);
		if (this.params.size() == 0) throw new OSException(OSException.NO_ARGS);
		
		File newDir = absPath(this.params.get(0), shellID);
		if (FileManager.existsDir(newDir)) {
			IOManager.getInstance().setWorkingDirectory(shellID, newDir);
			return "";
		}
		else {
			throw new OSException(OSException.UNFOUND_PATH);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean needParams() {
		return false;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void kill() throws OSException {
		
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String continueJob() throws OSException {
		this.shouldEnd = true;
		return null;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected String finalizeJob() throws OSException {
		return null;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String man(String command) {
	String man = "";
		
		man += "    changes the current working directory\n";
		man += "\n";
		man += "Command syntax:\n";
		man += "    "+command+" [path]\n";
		man += "\n";
		man += "Description:\n";
		man += "    .. - parent directory\n";
		man += "    . - current directory\n";
		man += "\n";
		man += "Examples:\n";
		man += "    "+command+" ..\n";
		man += "    "+command+" \\pictures\\greece\n";
		man += "    "+command+" ..\\downloads\n";
		
		return man;
	}

}
